-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
57 lines (50 loc) · 1.49 KB
/
Solution.java
File metadata and controls
57 lines (50 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.Stack;
import java.util.Scanner;
class Queue {
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>();
void enqueue(int value) {
s1.push(value);
}
int dequeue() {
if (s2.isEmpty()) {
while (!s1.isEmpty()) {
s2.push(s1.pop());
}
}
if (s2.isEmpty()) {
System.out.println("Queue is empty");
return -1;
}
return s2.pop();
}
}
public class QueueUsingTwoStacks {
public static void main(String[] args) {
Queue q = new Queue();
Scanner scanner = new Scanner(System.in);
int choice, value;
do {
System.out.println("\n1. Enqueue\n2. Dequeue\n3. Exit\nEnter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter value to enqueue: ");
value = scanner.nextInt();
q.enqueue(value);
break;
case 2:
value = q.dequeue();
if (value != -1)
System.out.println("Dequeued value: " + value);
break;
case 3:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice");
}
} while (choice != 3);
scanner.close();
}
}